package com.sky.controller.user;

import com.sky.entity.AddressBook;
import com.sky.result.Result;
import com.sky.service.AddressBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user/addressBook")
public class AddressBookController {
    @Autowired
    private AddressBookService addressBookService;
    @PostMapping
    public Result add(@RequestBody AddressBook addressBook) {
        addressBookService.add(addressBook);
        return Result.success();
    }
    @GetMapping("/list")
    public Result list() {
        return Result.success(addressBookService.list());
    }
    @PutMapping("/default")
    public Result setDefault(@RequestBody AddressBook addressBook) {
       long id = addressBook.getId();
        addressBookService.setDefault(id);
        return Result.success();
    }
    @PutMapping
    public Result update(@RequestBody AddressBook addressBook) {
        addressBookService.update(addressBook);
        return Result.success();
    }
    @GetMapping("{id}")
    public Result get(@PathVariable long id) {
        return Result.success(addressBookService.get(id));
    }
    @DeleteMapping
    public Result delete(@RequestParam long id) {
        addressBookService.deleteById(id);
        return Result.success();
    }
    @GetMapping("/default")
    public Result getDefault() {
        return Result.success(addressBookService.getDefault());
    }
}
